import axios from "./request";
import { DataType } from "./request";

//获取图表数据

export async function getChartData(questionId: string, componentId: string): Promise<DataType> {
  const res = (await axios.get(`/api/stat/${questionId}/${componentId}`)) as DataType;
  return res;
}

//获取统计列表

export async function getStatData(id: string, opt: optType): Promise<DataType> {
  const res = (await axios.get(`/api/stat/${id}`)) as DataType;
  return res;
}

//获取问卷信息
export async function getQuestionData(id: string): Promise<DataType> {
  const res = (await axios.get(`/api/question/${id}`)) as DataType;
  return res;
}
//创建问卷
export async function createQuestion(): Promise<DataType> {
  const res = (await axios.post("/api/questionpost")) as DataType;
  return res;
}

//获取问卷列表

type searchParams = {
  keyword: string;
  isStart: boolean;
  isDelete: boolean;
  pageSize: number;
  page: number;
};

export async function getQuestionList(params: Partial<searchParams>): Promise<DataType> {
  const url = "/api/questionpost/list";
  const res = (await axios.get(url, { params: params })) as DataType;
  return res;
}
//更新问卷信息
type optType = {
  [key: string]: any;
};

export async function updateQuestionData(id: string, opt: optType): Promise<DataType> {
  const res = await axios.patch(`/api/question/${id}`, opt);
  return res;
}

//复制问卷

export async function CopyQuestionData(id: string): Promise<DataType> {
  const res = await axios.post(`/api/question/copy/${id}`);
  return res;
}

//彻底删除
export async function DeleteQuestionData(ids: string[]): Promise<DataType> {
  const res = await axios.delete(`/api/question`, { data: { ids } });
  return res;
}

//登录信息

export async function GetUserInfo(): Promise<DataType> {
  const res = await axios.get(`/api/user/info`);
  return res;
}

//注册

type registerType = {
  username: string;
  nickname: string;
  password: string;
};

export async function RegisterUserInfo(opt: registerType): Promise<DataType> {
  const res = await axios.post(`/api/user/register`, opt);
  return res;
}

//登录
type LoginType = {
  username: string;
  password: string;
};

export async function LoginUserInfo(opt: LoginType): Promise<DataType> {
  const res = await axios.post(`/api/user/login`, opt);
  return res;
}
